package com.formula.propertyshop.view.common;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v4.app.FragmentActivity;

import com.formula.propertyshop.R;
import com.formula.propertyshop.utils.Const;
import com.formula.propertyshop.utils.ISlideMenuNofifier;
import com.formula.propertyshop.utils.WFSlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;

public abstract class BaseActivity extends FragmentActivity implements ISlideMenuNofifier{
	protected TopMenuBar topMenuBar ;
	protected ProgressDialog progressDlg;
	protected WFSlidingMenu slidingMenu;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initUI();
				
		topMenuBar = (TopMenuBar) findViewById(R.id.topMenu);
		topMenuBar.initUI();
		topMenuBar.updateUI();
		
		
		slidingMenu = new WFSlidingMenu(this);
		slidingMenu.setMode(SlidingMenu.LEFT);
		slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
		slidingMenu.setShadowWidthRes(R.dimen.shadow_width);
		slidingMenu.setBackgroundResource(R.drawable.homebg);
		slidingMenu.setShadowDrawable(R.drawable.shadow);
		
		//slidingMenu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
		slidingMenu.setBehindWidthRes(R.dimen.slidingmenu_offset);
		slidingMenu.setFadeDegree(0.35f);
		slidingMenu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
		slidingMenu.setMenu(R.layout.slidingmenumain);
        slidingMenu.setControls();
	}
	protected abstract void initUI();
	public void showProgressDlg(){
		if(progressDlg==null)
			progressDlg = ProgressDialog.show(this,getString(R.string.app_name).toUpperCase(), getString(R.string.loading), true, true);
		else
			progressDlg.show();
	}
	public void dismissProgressDlg(){
		try{
			if(progressDlg!=null && progressDlg.isShowing()){
				progressDlg.dismiss();
				progressDlg = null;
			}
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	public void showNetworkErrorDlg(){
		Intent intent = new Intent(this,NetWorkErrorActivity.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
		startActivityForResult(intent, Const.NETWORK_ACTIVITY_REQUEST_CODE);
	}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	    if (requestCode == Const.NETWORK_ACTIVITY_REQUEST_CODE) {
	    	if(resultCode == RESULT_OK){
	    		this.recreate();
	    	}
	    	else if(resultCode == Const.EXIT_KEY)
	    	{
	    		this.finish();
	    	}
	    }
	}
	public void showSlideMenu(){
		if(slidingMenu.isMenuShowing())
			slidingMenu.showContent();
		else
			slidingMenu.toggle();
	}
	public void onResume(){
		super.onResume();
		if(slidingMenu.isMenuShowing())
			slidingMenu.showContent();
	}
	protected void showLocationSettingDlg(int resString){
		
			AlertDialog.Builder dialog = new AlertDialog.Builder(this);
            dialog.setMessage(getResources().getString(resString));
            dialog.setPositiveButton(getResources().getString(R.string.msg_open_location_setting), new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface paramDialogInterface, int paramInt) {
                    // TODO Auto-generated method stub
                    Intent myIntent = new Intent( Settings.ACTION_LOCATION_SOURCE_SETTINGS );
                    startActivity(myIntent);
                    finish();
                }
            });          
            dialog.show();
		
	}
}
